﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Browser;
using System.Windows.Interop;
using System.Collections.Generic;
//xml handling namespaces
using System.Linq;
using Microsoft.SharePoint.Client;

namespace Microsoft.ProductivityHub.SilverlightProductCarousel
{
	public partial class MainPage : UserControl
	{

        private int ViewState = 0; 
        DataLoader videoItemsLoader = new DataLoader();
        List<ProductItem> VideoList;


        // --------------------------------------------------------------------------------------
        // INITIALIZE COMPONENT
        // --------------------------------------------------------------------------------------
        public MainPage(string videoLibraryPath)
		{
			// Required to initialize variables
			InitializeComponent();

            // Configure Carousel
            SetViewState(0);

            videoItemsLoader._Loaded += new DataLoader._DataLoaded(VideoItemsLoader_DataLoaded);
            videoItemsLoader.LoadData(videoLibraryPath);
		}




        // --------------------------------------------------------------------------------------
        // BUILD CAROUSEL
        // --------------------------------------------------------------------------------------
        //
        // This section reads the list data and builds the carousel based on the returned information

        private void VideoItemsLoader_DataLoaded(System.Collections.Generic.List<ProductItem> videos)
        {
            videoItemsLoader._Loaded -= new DataLoader._DataLoaded(VideoItemsLoader_DataLoaded);

            if (videos != null && videos.Count > 0)
            {
                this.VideoList = videos;
                PopulateCarousel();
            }
            else
            {
                // TBD on what happens if no products are found in the products list
            }

        }

        // Add Carousel Items to Carousel based on VideoList
        private void PopulateCarousel()
        {
            MyVideoCarousel.CreateList(VideoList.Count);

            for (int i = 0; i < VideoList.Count; i++)
            {
                CarouselItem ThisItem = new CarouselItem();
                GenerateCarouselItem(ThisItem, VideoList[i], i);
                ThisItem.ClickItem += new CarouselItem.ClickItemEvent(CarouselItem_ClickItem);
                MyVideoCarousel.CarouselList.Add(ThisItem);
                MyVideoCarousel.CarouselCanvas.Children.Add(ThisItem);
            }

            MyVideoCarousel.StartCarousel();
        }

        void CarouselItem_ClickItem(ProductItem clickedProduct)
        {
            string productName = clickedProduct.Title;
            string currentSiteUrl = ApplicationContext.Current.Url;
            Uri targetUri = new Uri(String.Format("{0}/Pages/Product.aspx?Product={1}", currentSiteUrl, productName));

            HtmlPage.Window.Navigate(targetUri);
        }

        

        private void GenerateCarouselItem(CarouselItem carouselItem, ProductItem sourceProduct, int listIndex)
        {
            carouselItem.Index = listIndex;
            carouselItem.Data = sourceProduct;
            carouselItem.Title = sourceProduct.FullTitle;

            carouselItem.LoadThumbnail(sourceProduct.ThumbnailUri);
            carouselItem.LoadReflection(sourceProduct.ThumbnailUri);


        }

       

        // --------------------------------------------------------------------------------------
        // VIEW STATE FUNCTIONS
        // --------------------------------------------------------------------------------------
        //
        // Note: there is only 1 view state: 0 = carousel 

        private void ChangeView(int NewView) // event
        {
            SetViewState(NewView);
        }


        private void SetViewState(int NewView)
        {
            switch (NewView)
            {
                default:
                case 0:
                    ShowCarouselView();
                    break;

            }
        }

        // Show View 0
        private void ShowCarouselView()
        {
            ShowCarousel_SB.Begin();
            ViewState = 0;
        }

       

       



        

// ---------------------------------------------------------------------------------
// END CLASS
	}
}